<?php
class Provider extends CI_Controller {
	
	private $map, $logo;
	
	function __construct() {
		parent::__construct ();
		$username = $this->session->userdata('username');
		$level = $this->session->userdata('level');
		if($username !='' && $level == 1 ){
			$this->load->model ( 'be/ProviderModel' );
			$this->load->model ( 'be/sub_table' );
			$this->load->helper ( 'form' );
			$this->load->helper ( 'url' );
			$this->load->library ( 'form_validation' );
		}
		else
		{
			redirect(base_url().'index.php/user');
		}
		
	}
	
	public function index() {
		if(isset($_POST['search'])){
			$data ['provider'] = $this->ProviderModel->getbyfillter ($_POST['category'],$_POST['pricePolicy'],$_POST['timePolicy'],$_POST['level'],$_POST['province'],trim($_POST['phone']),trim($_POST['name']));
		}
		else{
			$data ['provider'] = $this->ProviderModel->getProvider ();
		}
		$data ['page_title'] = 'Nhà cung cấp';
		$data ['category'] = $this->sub_table->getCategory ();
		$data ['timePolicy'] = $this->sub_table->getTimePolicy ();
		$data ['pricePolicy'] = $this->sub_table->getPricePolicy ();
		$data ['level'] = $this->sub_table->getLevel ();
		$data ['province'] = $this->sub_table->getProvince();
		$data ['main_content'] = 'be/provider/views';
		$this->load->view ( 'be/includes/template', $data );
	}

	public function create() {
		$this->form_validation->set_rules ( 'name', 'Tên nhà cung cấp', 'required' );
		$this->form_validation->set_rules ( 'country', 'Quốc gia' );
		$this->form_validation->set_rules ( 'email', 'Email', 'required|valid_email' );
		$this->form_validation->set_rules ( 'phone', 'Điện thoại', 'required|min_length[8]|max_length[15]' );
		$this->form_validation->set_rules ( 'address', 'Địa chỉ', 'required' );
		$this->form_validation->set_rules ( 'district', 'Quận/Huyện', 'required' );
		$this->form_validation->set_rules ( 'province', 'Tỉnh/Thành phố', 'required' );
		$this->form_validation->set_rules ( 'status', 'Trạng thái' );
		$this->form_validation->set_rules ( 'logo', 'Logo', 'callback_get_logo_uploaded' );
		$this->form_validation->set_rules ( 'map', 'Bản đồ', 'callback_get_map_uploaded' );
		$this->form_validation->set_rules ( 'userPolicy', 'Lưu ý khách hàng' );
		$this->form_validation->set_rules ( 'policy', 'Điều kiện sử dụng' );
		$this->form_validation->set_rules ( 'introduction', 'Giới thiệu' );
		$this->form_validation->set_rules ( 'level', 'Cấp', 'required|is_natural_no_zero' );
		$datatemp ['list_category'] = $this->sub_table->getCategory ();
		$datatemp ['list_timePolicy'] = $this->sub_table->getTimePolicy ();
		$datatemp ['list_pricePolicy'] = $this->sub_table->getPricePolicy ();

		if ($this->form_validation->run () == FALSE) {
			$data ['page_title'] = 'Thêm mới nhà cung cấp';
			$data ['main_content'] = 'be/provider/create';
			$data ['list_category'] = $this->sub_table->getCategory ();
			$data ['list_timePolicy'] = $this->sub_table->getTimePolicy ();
			$data ['list_pricePolicy'] = $this->sub_table->getPricePolicy ();
			$data ['list_level'] = $this->sub_table->getLevel ();
			$this->load->view ( 'be/includes/template', $data );
		} else {
			$data ['name'] = $this->input->post ( 'name' );
			$data ['phone'] = $this->input->post ( 'phone' );
			$data ['email'] = $this->input->post ( 'email' );
			$data ['address'] = $this->input->post ( 'address' );
			$data ['district'] = $this->input->post ( 'district' );
			$data ['province'] = $this->input->post ( 'province' );
			$data ['country'] = $this->input->post ( 'country' );
			$data ['policy'] = $this->input->post ( 'policy' );
			$data ['userPolicy'] = $this->input->post ( 'userPolicy' );
			$data ['introduction'] = $this->input->post ( 'introduction' );


			$data ['logo'] = $this->logo;
			$data ['map'] = $this->map;
			$data ['status'] = $this->input->post ( 'status' );
			$data ['level'] = $this->input->post ('level');
			
			if (! $this->ProviderModel->create ( $data )) {
				$data ['page_title'] = 'Có lỗi xảy ra';
				$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/provider/create"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/provider">Trở lại trang danh sách</a></p>';
				$data ['message_detail'] = $mess_detail;
			} else {
				$data ['page_title'] = 'Thành công';
				$mess_detail = '<p>Thêm mới nhà cung cấp thành công!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/provider/create">Tiếp tục</a> hoặc <a href="' . base_url () . 'index.php/provider">Trở lại trang danh sách</a>';
				$data ['message_detail'] = $mess_detail;
				
				//get newId of provider have inserted
				$newId = $this->ProviderModel->getNewestId();

				foreach($datatemp ['list_category'] as $item):
					$name_temp = 'cate'.$item['id'];
					if( isset($_POST[$name_temp])){
						$this->sub_table->insertCategory($newId,$_POST[$name_temp]);
					}
				endforeach;

				// make new pp list
				foreach($datatemp ['list_pricePolicy'] as $item):
					$name_temp = 'pp'.$item['id'];
					if( isset($_POST[$name_temp])){
						$this->sub_table->insertPricePolicy($newId,$_POST[$name_temp]);
					}
				endforeach;

				// make new tp list
				foreach($datatemp ['list_timePolicy'] as $item):
					$name_temp = 'tp'.$item['id'];
					if( isset($_POST[$name_temp])){
						$this->sub_table->insertTimePolicy($newId,$_POST[$name_temp]);
					}
				endforeach;

			}
			$data ['main_content'] = 'be/message';
			$this->load->view ( 'be/includes/template', $data );
		}
	}
	
	public function view($id) {
		$provider_item = $this->ProviderModel->get ( $id );
		$data ['page_title'] = 'View Provider';
		$data ['name'] = $provider_item->name;
		$data ['logo'] = $provider_item->logo;
		$data ['phone'] = $provider_item->phone;
		$data ['email'] = $provider_item->email;
		$data ['address'] = $provider_item->address;
		$data ['district'] = $provider_item->district;
		$data ['province'] = $provider_item->province;
		$data ['country'] = $provider_item->country;
		$data ['map'] = $provider_item->map;
		$data ['policy'] = $provider_item->policy;
		$data ['userPolicy'] = $provider_item->userPolicy;
		$data ['introduction'] = $provider_item->introduction;
		$data ['status'] = $provider_item->status;
		
		$data ['category'] = $this->sub_table->getCategory ( $id );
		$data ['pricePolicy'] = $this->sub_table->getPricePolicy ( $id );
		$data ['timePolicy'] = $this->sub_table->getTimePolicy ( $id );
		$data ['level'] = $provider_item->level;
		
		$data ['list_category'] = $this->sub_table->getCategory ();
		$data ['list_timePolicy'] = $this->sub_table->getTimePolicy ();
		$data ['list_pricePolicy'] = $this->sub_table->getPricePolicy ();
		$data ['list_level'] = $this->sub_table->getLevel ();
		$data ['id'] = $id;
		
		$data ['main_content'] = 'be/provider/item';
		$this->load->view ( 'be/includes/template', $data );
	}
	

	function delete() {
		$ids = $this->input->post('selected');
		foreach ($ids as $id){
			$this->ProviderModel->delete($id);
		}
		redirect(site_url('provider'));
	}

	function update($id) {
		$provider_item = $this->ProviderModel->get ( $id );
		$data ['name'] = $provider_item->name;
		$data ['logo'] = $provider_item->logo;
		$data ['level'] = $provider_item->level;
		$data ['category'] = $this->sub_table->getCategory ( $id );
		$data ['pricePolicy'] = $this->sub_table->getPricePolicy ( $id );
		$data ['timePolicy'] = $this->sub_table->getTimePolicy ( $id );
		$data ['phone'] = $provider_item->phone;
		$data ['email'] = $provider_item->email;
		$data ['address'] = $provider_item->address;
		$data ['district'] = $provider_item->district;
		$data ['province'] = $provider_item->province;
		$data ['country'] = $provider_item->country;
		$data ['map'] = $provider_item->map;
		$data ['policy'] = $provider_item->policy;
		$data ['userPolicy'] = $provider_item->userPolicy;
		$data ['introduction'] = $provider_item->introduction;
		$data ['list_category'] = $this->sub_table->getCategory ();
		$data ['list_timePolicy'] = $this->sub_table->getTimePolicy ();
		$data ['list_pricePolicy'] = $this->sub_table->getPricePolicy ();
		$data ['list_level'] = $this->sub_table->getLevel ();
		$data ['id'] = $id;
		
		$this->form_validation->set_rules ( 'name', 'Tên nhà cung cấp', 'required' );
		$this->form_validation->set_rules ( 'country', 'Quốc gia' );
		$this->form_validation->set_rules ( 'email', 'Email', 'required|valid_email' );
		$this->form_validation->set_rules ( 'phone', 'Điện thoại', 'required|min_length[8]|max_length[15]' );
		$this->form_validation->set_rules ( 'address', 'Địa chỉ', 'required' );
		$this->form_validation->set_rules ( 'district', 'Quận/Huyện', 'required' );
		$this->form_validation->set_rules ( 'province', 'Tỉnh/Thành phố', 'required' );
		$this->form_validation->set_rules ( 'status', 'Trạng thái' );
		$this->form_validation->set_rules ( 'logo', 'Logo', 'callback_get_logo_uploaded' );
		$this->form_validation->set_rules ( 'map', 'Bản đồ', 'callback_get_map_uploaded' );
		$this->form_validation->set_rules ( 'userPolicy', 'Lưu ý khách hàng' );
		$this->form_validation->set_rules ( 'policy', 'Điều kiện sử dụng' );
		$this->form_validation->set_rules ( 'introduction', 'Giới thiệu' );
		$this->form_validation->set_rules ( 'level', 'Cấp', 'required|is_natural_no_zero' );
		
		if ($this->form_validation->run () == FALSE) {
			$data ['page_title'] = 'Cập nhật nhà cung cấp';
			$data ['main_content'] = 'be/provider/update';
			$this->load->view ( 'be/includes/template', $data );
		} 
		else {
			$providerdata ['id'] = $id;
			$providerdata ['name'] = $this->input->post ( 'name' );
			//$data['category'] = $this->input->post ( 'category' );
			//$data['timePolicy'] = $this->input->post ( 'timePolicy' );
			//$data['pricePolicy'] = $this->input->post ( 'pricePolicy' );
			$providerdata ['phone'] = $this->input->post ( 'phone' );
			$providerdata ['email'] = $this->input->post ( 'email' );
			$providerdata ['address'] = $this->input->post ( 'address' );
			$providerdata ['district'] = $this->input->post ( 'district' );
			$providerdata ['province'] = $this->input->post ( 'province' );
			$providerdata ['country'] = $this->input->post ( 'country' );
			$providerdata ['policy'] = $this->input->post ( 'policy' );
			$providerdata ['userPolicy'] = $this->input->post ( 'userPolicy' );
			$providerdata ['introduction'] = $this->input->post ( 'introduction' );
			$providerdata ['level'] = $this->input->post ( 'level' );
			$providerdata ['status'] = $this->input->post ( 'status' );

			

			
			if($this->logo == PROVIDER_DEFAULT_LOGO) {
				$providerdata['logo'] = $provider_item->logo;
			}
			else {
				$providerdata['logo'] = $this->logo;	
			}		
			if($this->map == PROVIDER_DEFAULT_MAP) {
				$providerdata['map'] = $provider_item->map;
			}
			else {
				$providerdata['map'] = $this->map;	
			}		
			
			if (! $this->ProviderModel->update ( $id, $providerdata )) {
				$data ['page_title'] = 'Có lỗi xảy ra';
				$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/provider/update"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/provider">Trở lại trang danh sách</a></p>';
				$data ['message_detail'] = $mess_detail;
			} else {
				$data ['page_title'] = 'Thành công';
				$mess_detail = '<p>Cập nhật nhà cung cấp thành công!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/provider/update">Tiếp tục</a> hoặc <a href="' . base_url () . 'index.php/provider">Trở lại trang danh sách</a>';
				$data ['message_detail'] = $mess_detail;


				$this->sub_table->deleteCategory($data['id']);
				foreach($data ['list_category'] as $item):
					$name_temp = 'cate'.$item['id'];
					if( isset($_POST[$name_temp])){
						$this->sub_table->insertCategory($data['id'],$_POST[$name_temp]);
					}
				endforeach;

				// make new pp list
				$this->sub_table->deletePricePolicy($data['id']);
				foreach($data ['list_pricePolicy'] as $item):
					$name_temp = 'pp'.$item['id'];
					if( isset($_POST[$name_temp])){
						$this->sub_table->insertPricePolicy($data['id'],$_POST[$name_temp]);
					}
				endforeach;

				// make new tp list
				$this->sub_table->deleteTimePolicy($data['id']);
				foreach($data ['list_timePolicy'] as $item):
					$name_temp = 'tp'.$item['id'];
					if( isset($_POST[$name_temp])){
						$this->sub_table->insertTimePolicy($data['id'],$_POST[$name_temp]);
					}
				endforeach;
			}
			
			$data ['main_content'] = 'be/message';
			$this->load->view ( 'be/includes/template', $data );
		}
	}
	
	function get_logo_uploaded() {
		//Create location if it does not exist
		$location = DEFAULT_IMAGE_UPLOAD_PATH . '/provider';
		if (! is_dir ( $location )) {
			if (! mkdir ( $location, 0755 )) {
				die ( 'Failed to create folders...' );
			}
		}
		$config ['upload_path'] = $location;
		$config ['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config ['max_size'] = '10000';
		
		$this->load->library ( 'upload', $config );
		if ($this->upload->do_upload ( 'logo' )) {
			$tmp = $this->upload->data ( 'logo' );
			$this->logo = $location . '/' . $tmp ['file_name'];
		} else {
			$this->logo = PROVIDER_DEFAULT_LOGO;
		}
	}
	
	function get_map_uploaded() {
		//Create location if it does not exist
		$location = DEFAULT_IMAGE_UPLOAD_PATH . '/provider';
		if (! is_dir ( $location )) {
			if (! mkdir ( $location, 0755 )) {
				die ( 'Failed to create folders...' );
			}
		}
		$config ['upload_path'] = $location;
		$config ['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config ['max_size'] = '10000';
		
		$this->load->library ( 'upload', $config );
		if ($this->upload->do_upload ( 'map' )) {
			$tmp = $this->upload->data ( 'map' );
			$this->map = $location . '/' . $tmp ['file_name'];
		} else {
			$this->map = PROVIDER_DEFAULT_MAP;
		}
	}
}
